﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace DynamicCode
{
    public class ExeclArrayFunctons
    {

        public static object VLOOKUP<T>(IList<T> source, object key, string propertyName)
        {
            throw new NotImplementedException();
           //convert to code  return  source.FirstOrDefault(n => n.Key == Key)?.propertyName;
        }
    }
    public class ExeclFunctons
    {
        public static int LEN(string str)
        {
            return str == null ? 0 : str.Length;
        }
        public static decimal MOD(decimal d, decimal decimals)
        {
            return d % decimals;
        }
        public static double MOD(double d, double decimals)
        {
            return d % decimals;
        }
        public static float MOD(float d, float decimals)
        {
            return d % decimals;
        }
        public static int MOD(int d, int decimals)
        {
            return d % decimals;
        }
        public static long MOD(long d, long decimals)
        {
            return d % decimals;
        }
        public static string MID(string txt, int start, int length)
        {
            if (string.IsNullOrEmpty(txt) || txt.Length > start) return "";
            var index = start - 1;
            if (index + length > txt.Length) return txt.Substring(index);
            return txt.Substring(index, length);
        }
        public static string BIRTHDAY(string idcard)
        {
            if (string.IsNullOrEmpty(idcard) || idcard.Length != 18) return "身份证错误";
            return MID(idcard, 7, 8);
        }
        public static string GENDER(string idcard)
        {
            if (string.IsNullOrEmpty(idcard) || idcard.Length != 18) return "身份证错误";
            return int.Parse(MID(idcard, 17, 1)) % 2 == 0 ? "女" : "男";
        }
        public static string AGE(string idcard)
        {
            if (string.IsNullOrEmpty(idcard) || idcard.Length != 18) return "身份证错误";
            var bathday = MID(idcard, 7, 8);
            if (bathday.Length == 8) return new DateTime((DateTime.Now - DateTime.ParseExact(bathday, "yyyyMMdd", null)).Ticks).Year.ToString();
            return "身份证错误";
        }
        public static decimal ROUND(decimal d, int decimals)
        {
            return Math.Round(d, decimals);
        }
        public static double ROUND(double d, int decimals)
        {
            return Math.Round(d, decimals);
        }
        public static object IF(bool condition, object trueValue, object falseValue)
        {
            return condition ? trueValue : falseValue;
        }
        public static bool AND(params bool[] conditions)
        {
            return conditions != null && conditions.All(n => n);
        }
        public static bool OR(params bool[] conditions)
        {
            return conditions != null && conditions.Any(n => n);
        }
        public static double MAX(params int[] numbers)
        {
            return numbers.Max();
        }
        public static double MAX(params double[] numbers)
        {
            return numbers.Max();
        }
        public static decimal MAX(params decimal[] numbers)
        {
            return numbers.Max();
        }
        public static double MAX(params long[] numbers)
        {
            return numbers.Max();
        }
        public static float MAX(params float[] numbers)
        {
            return numbers.Max();
        }
        public static double MIN(params int[] numbers)
        {
            return numbers.Min();
        }
        public static double MIN(params double[] numbers)
        {
            return numbers.Min();
        }
        public static decimal MIN(params decimal[] numbers)
        {
            return numbers.Min();
        }
        public static double MIN(params long[] numbers)
        {
            return numbers.Min();
        }
        public static float MIN(params float[] numbers)
        {
            return numbers.Min();
        }
        public static double AVERAGE(params int[] numbers)
        {
            return numbers.Average();
        }
        public static double AVERAGE(params double[] numbers)
        {
            return numbers.Average();
        }
        public static decimal AVERAGE(params decimal[] numbers)
        {
            return numbers.Average();
        }
        public static double AVERAGE(params long[] numbers)
        {
            return numbers.Average();
        }
        public static float AVERAGE(params float[] numbers)
        {
            return numbers.Average();
        }
        public static int COUNTIF(Func<int, bool> condition, params int[] numbers)
        {
            return numbers.Where(condition).Count();
        }
        public static int COUNTIF(Func<double, bool> condition, params double[] numbers)
        {
            return numbers.Where(condition).Count();
        }
        public static int COUNTIF(Func<decimal, bool> condition, params decimal[] numbers)
        {
            return numbers.Where(condition).Count();
        }
        public static int COUNTIF(Func<long, bool> condition, params long[] numbers)
        {
            return numbers.Where(condition).Count();
        }
        public static int COUNTIF(Func<float, bool> condition, params float[] numbers)
        {
            return numbers.Where(condition).Count();
        }
        public static int COUNT(params int[] numbers)
        {
            return numbers.Length;
        }
        public static int COUNT(params double[] numbers)
        {
            return numbers.Length;
        }
        public static int COUNT(params decimal[] numbers)
        {
            return numbers.Length;
        }
        public static int COUNT(params long[] numbers)
        {
            return numbers.Length;
        }
        public static int COUNT(params float[] numbers)
        {
            return numbers.Length;
        }
        public static int SUM(params int[] numbers)
        {
            return numbers.Sum();
        }
        public static double SUM(params double[] numbers)
        {
            return numbers.Sum();
        }
        public static decimal SUM(params decimal[] numbers)
        {
            return numbers.Sum();
        }
        public static long SUM(params long[] numbers)
        {
            return numbers.Sum();
        }
        public static float SUM(params float[] numbers)
        {
            return numbers.Sum();
        }
        public static float SUM(params float?[] numbers)
        {
            return numbers.Where(n => n != null).Sum().Value;
        }
        public static int SUM(params int?[] numbers)
        {
            return numbers.Where(n => n != null).Sum().Value;
        }
        public static double SUM(params double?[] numbers)
        {
            return numbers.Where(n => n != null).Sum().Value;
        }
        public static decimal SUM(params decimal?[] numbers)
        {
            return numbers.Where(n => n != null).Sum().Value;
        }
        public static long SUM(params long?[] numbers)
        {
            return numbers.Where(n => n != null).Sum().Value;
        }
        public static double SIN(double number)
        {
            return Math.Sin(number);
        }
        public static double SIN(decimal number)
        {
            return Math.Sin((double)number);
        }
        public static double SIN(float number)
        {
            return Math.Sin((double)number);
        }
        public static double ABS(double number)
        {
            return Math.Abs(number);
        }
        public static decimal ABS(decimal number)
        {
            return Math.Abs(number);
        }
        public static short ABS(short number)
        {
            return Math.Abs(number);
        }
        public static int ABS(int number)
        {
            return Math.Abs(number);
        }
        public static long ABS(long number)
        {
            return Math.Abs(number);
        }
        public static nint ABS(nint number)
        {
            return Math.Abs(number);
        }
        public static sbyte ABS(sbyte number)
        {
            return Math.Abs(number);
        }
        public static float ABS(float number)
        {
            return Math.Abs(number);
        }
    }
}
